﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace SoundServant
{
    public static class Library
    {
        static ArrayList recordings = new ArrayList();
        static ArrayList recordingIndexes = new ArrayList();
        static ArrayList recordingLoans = new ArrayList();

        public delegate void LibraryRecordingDeletedEventHandler(StoredRecording recording);
        static public event LibraryRecordingDeletedEventHandler LibraryRecordingDeleted;

        public delegate void VoidEventHandler();
        static public event VoidEventHandler RecordingsUpdated;

        public static StoredRecording OpenRecording(int id)
        {
            StoredRecording recordingToReturn = null;
            if (recordingIndexes.Contains(id))
            {
                int index = recordingIndexes.IndexOf(id);
                recordingLoans[index] = (int)recordingLoans[index] + 1;
                recordingToReturn = (StoredRecording)recordings[index];
            }
            else
            {
                recordingToReturn = new StoredRecording(id);
                recordings.Add(recordingToReturn);
                recordingIndexes.Add(id);
                int loansInt = 1;
                recordingLoans.Add(loansInt);
            }
            Logs.Information("Library", "Recording booked out - Recording Id: " + recordingToReturn.Id + " Copies Now Out: " + recordingLoans[recordingIndexes.IndexOf(id)]);
            return recordingToReturn;
        }

        public static void BookAnotherCopyOut(StoredRecording recording)
        {
            int index = recordings.IndexOf(recording);
            recordingLoans[index] = (int)recordingLoans[index] + 1;
            Logs.Information("Library", "Another copy booked out - Recording Id: " + recording.Id + " Copies Now Out: " + recordingLoans[index]);
        }

        public static void ReturnRecording(StoredRecording recordingToReturn)
        {
            int index = recordings.IndexOf(recordingToReturn);

            if (index > -1)
            {
                recordingLoans[index] = ((int)recordingLoans[index]) - 1;

                Logs.Information("Library", "Recording returned - Recording Id: " + recordingToReturn.Id + " Copies Still Out: " + recordingLoans[index]);

                if ((int)recordingLoans[index] == 0)
                {
                    recordings.Remove(recordingToReturn);
                    recordingIndexes.RemoveAt(index);
                    recordingLoans.RemoveAt(index);
                    Logs.Information("Library", "Recording disposed - Recording Id: " + recordingToReturn.Id);
                }
            }
        }

        public static StoredRecording NewRecording(MeetingType type, Congregation congregation, Recorder recorder)
        {
            StoredRecording newRecording = new StoredRecording(type, congregation, recorder);
            recordings.Add(newRecording);
            recordingIndexes.Add(newRecording.Id);
            int loansInt = 1;
            recordingLoans.Add(loansInt);
            Logs.Information("Library", "New recording - Recording Id: " + newRecording.Id);
            if (RecordingsUpdated != null) RecordingsUpdated();
            return newRecording;
        }

        public static bool DeleteRecording(StoredRecording recordingToDelete)
        {
            int index = recordings.IndexOf(recordingToDelete);

            if (recordingToDelete.Locks == 0)
            {
                recordings.Remove(recordingToDelete);
                recordingToDelete.Delete();
                LibraryRecordingDeleted(recordingToDelete);
                if (index != -1)
                {
                    recordingLoans.RemoveAt(index);
                    recordingIndexes.RemoveAt(index);
                }
                return true;
            }
            else
            {
                SSMessageBox.Show("Error", "This recording cannot be deleted. It contains locks.");
                return false;
            }
        }

        public static void RestartLibrary()
        {
            recordings.Clear();
            recordingLoans.Clear();
            recordingIndexes.Clear();
            Logs.Information("Library", "Restarting Library");
        }
    }
}
